package com.xx566.blog.common;

import com.alibaba.druid.filter.stat.MergeStatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.*;
import com.jfinal.ext.handler.FakeStaticHandler;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;
import com.xx566.blog.controller.AdminController;
import com.xx566.blog.controller.FileUploadController;
import com.xx566.blog.controller.XX566Controller;
import com.xx566.blog.model.Article;
import com.xx566.blog.model.Friendly;
import com.xx566.blog.model.Tags;
import com.xx566.blog.model.User;

/**
 * 系统配置
 * 
 * @Package com.xx566.blog.common
 * @ClassName: XX566Config
 * @author Realfighter
 * @date 2014-6-7 下午03:22:04
 */
public class XX566Config extends JFinalConfig {

	/**
	 * 配置常量
	 * 
	 * @Title: configConstant
	 * @author Realfighter
	 * @date 2014-6-7 下午03:21:56
	 * @param me
	 */
	@Override
	public void configConstant(Constants me) {
		// loadPropertyFile("jdbc-config.txt");
		me.setDevMode(Boolean.parseBoolean(getProperty("devMode")));
		me.setViewType(ViewType.JSP);
		me.setError404View("/common/error_404.jsp");
		me.setError500View("/common/error_500.jsp");
	}

	/**
	 * 配置处理器
	 * 
	 * @Title: configHandler
	 * @author Realfighter
	 * @date 2014-6-7 下午03:22:22
	 * @param me
	 */
	@Override
	public void configHandler(Handlers me) {
		me.add(new FakeStaticHandler());
	}

	/**
	 * 配置全局拦截器
	 * 
	 * @Title: configInterceptor
	 * @author Realfighter
	 * @date 2014-6-7 下午03:23:12
	 * @param me
	 */
	@Override
	public void configInterceptor(Interceptors me) {
	}

	/**
	 * 配置插件
	 * 
	 * @Title: configPlugin
	 * @author Realfighter
	 * @date 2014-6-7 下午03:23:44
	 * @param me
	 */
	@Override
	public void configPlugin(Plugins me) {
		// 配置缓存插件
		EhCachePlugin ehCachePlugin = new EhCachePlugin();
		me.add(ehCachePlugin);

		// 配置C3p0数据库连接池插件
		// C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"),
		// getProperty("user"), getProperty("password").trim());
		// me.add(c3p0Plugin);

		DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbcUrl"),
				getProperty("user"), getProperty("password").trim());
		druidPlugin.addFilter(new MergeStatFilter());
		druidPlugin.setMaxActive(150);
		WallFilter wall = new WallFilter();
		wall.setDbType(getProperty("dbType"));
		druidPlugin.addFilter(wall);
		me.add(druidPlugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		me.add(arp);

		// 添加映射
		arp.addMapping("friendly", Friendly.class);
		arp.addMapping("tags", Tags.class);
		arp.addMapping("user", User.class);
		arp.addMapping("article", Article.class);
		arp.addMapping("message", com.xx566.blog.model.Message.class);
	}

	/**
	 * 配置路由
	 * 
	 * @Title: configRoute
	 * @author Realfighter
	 * @date 2014-6-7 下午03:23:34
	 * @param me
	 */
	@Override
	public void configRoute(Routes me) {
		me.add("/", XX566Controller.class, "/WEB-INF/pages");
		me.add("/admin", AdminController.class, "/WEB-INF/pages");
		me.add("/fileUpload", FileUploadController.class, "/");
	}

	/**
	 * 重新配置文件读取方法
	 * 
	 * @Title: getProperty
	 * @author Realfighter
	 * @date 2014-6-19 上午10:44:41
	 * @param key
	 * @return
	 */
	@Override
	public String getProperty(String key) {
		return com.xx566.blog.common.XX566Utils.getProperty(key);
	}
}
